/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

package com.levelmaster2000
{
    import com.pblabs.engine.entity.IEntity;
    import com.pblabs.engine.PBE;
    import com.pblabs.engine.debug.Logger;
    
    public class Selection
    {
        private var _selection:Array = new Array();
        
        public function Selection()
        {
            
        }
        
        public function get count():int
        {
            return _selection.length;
        }
        
        public function at(index:int):IEntity
        {
            if(index > count)
            {
                Logger.print(this, "Index out of bounds at: " + index);
                return null;
            }
            
            return _selection[index];
        }
        
        public function clear(fireSelectionChangedEvent:Boolean = true):void
        {
            _selection = new Array();
            
            if(fireSelectionChangedEvent)
            {
                selectionChanged();
            }
        }
        
        public function addToSelection(object:IEntity, clearArray:Boolean):void
        {
            if(clearArray)
            {
                _selection = new Array();
            }
            
            _selection.push(object);
            
            selectionChanged();
        }
        
        //TODO: needs testing!
        public function addMultipleToSelection(objects:Array, clearArray:Boolean):void
        {
            if(clearArray)
            {
                _selection = new Array();
            }
            
            for each( var object:IEntity in objects )
            {
                _selection.push(object);
            }
            
            selectionChanged();
        }
        
        //TODO: needs testing!
        public function removeFromSelectionByName(objectName:String):void
        {
            for each( var object:IEntity in _selection )
            {
                if(object.name == objectName)
                {
                    removeFromSelection(object);
                    return;
                }
            }
            
            selectionChanged();
        }

        //TODO: needs testing!
        public function removeFromSelection(object:IEntity):void
        {
            var idx:int = _selection.indexOf(object);
            if( idx >= 0 )
            {
                _selection.splice(idx, 1);
            }
            
            selectionChanged();
        }

        private function selectionChanged():void
        {
            PBE.mainStage.dispatchEvent(new SelectionChangedEvent(SelectionChangedEvent.SELECTION_CHANGED, _selection));
        }
    }
}